package com.zyn.java8.lambda;

import java.util.concurrent.Callable;

/**
 * Lambda多线程
 * 
 * @author tongyufu
 *
 */
public class RunnableDemo {

    public static void main(String[] args) throws Exception {
        RunnableDemo.runnable();
        RunnableDemo.callable();
    }

    public static void runnable() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("JDK1.8之前的写法");
            }
        }).start();

        new Thread(() -> System.out.println("Lambda写法，只有一行代码时可省略{}")).start();
        new Thread(() -> {
            System.out.print("Lambda写法，");
            System.out.println("多行代码时需要{}");
        }).start();
    }

    public static void callable() throws Exception {
        //返回值(省略了return)
        Callable<String> call = () -> "返回值：s";
        System.out.println(call.call());

        //嵌套的λ表达式
        Callable<Runnable> call2 = () -> () -> {
            System.out.println("嵌套的λ表达式");
        };
        call2.call().run();

        //用在条件表达式中
        Callable<Integer> call3 = "a".equals("b") ? (() -> 24) : (() -> 42);
        System.out.println("条件表达式中：" + call3.call());
    }
}
